package cn.flying.cloud.base.common.utils.lang;

import java.util.HashMap;
import java.util.Map;

/**
 * ThreadLocal 工具类
 *
 * @author: admin
 * @date: 2022年06月20日 20:29
 * @version: 1.0
 */
public class ThreadLocalUtil {

    private static final ThreadLocal<Map<String, Object>> threadLocal = ThreadLocal.withInitial(() -> new HashMap<>(100));

    public static Map<String, Object> getThreadLocal() {
        return threadLocal.get();
    }

    public static Object get(String key) {
        Map<String, Object> map = threadLocal.get();
        return map.get(key);
    }

    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        map.put(key, value);
    }

    public static void set(Map<String, Object> objectMap) {
        Map<String, Object> map = threadLocal.get();
        map.putAll(objectMap);
    }

    public static void remove() {
        threadLocal.remove();
    }

    public static <T> T remove(String key) {
        Map<String, Object> map = threadLocal.get();
        return (T) map.remove(key);
    }

}